<?php
namespace App\Api;
use PhalApi\Api;

/**
 * 默认接口服务类
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Site extends Api {
    public function getRules() {
        return array(
            'index' => array(
                'username'  => array('name' => 'username', 'default' => 'PhalApi', 'desc' => '用户名'),
            ),
            'select'       => array(
                'page'  => array('name' => 'page', 'require' => true, 'desc' => '当前页'),
                'size'  => array('name' => 'size', 'require' => true, 'desc' => '每页显示多少'),
                'table' => array('name' => 'table', 'require' => false, 'desc' => '表名'),
            ),
            "select_where" => array(
                'where' => array('name' => 'where', 'require' => true, 'desc' => '查询条件'),
                'table' => array('name' => 'table', 'require' => true, 'desc' => '表名'),
                'page'  => array('name' => 'page', 'require' => true, 'desc' => '当前页'),
                'size'  => array('name' => 'size', 'require' => true, 'desc' => '每页显示多少'),
                'order' => array('name' => 'order', 'require' => false, 'desc' => '排序'),
            ),
            "update"       => array(
                'id'    => array('name' => 'id', 'require' => false, 'desc' => 'id'),
                'arr'   => array('name' => 'arr', 'require' => true, 'desc' => 'JSON数据'),
                'table' => array('name' => 'table', 'require' => true, 'desc' => '表名'),
            ),
            "delete"       => array(
                'id'    => array('name' => 'id', 'require' => false, 'desc' => 'id'),
                'table' => array('name' => 'table', 'require' => true, 'desc' => '表名'),
            ),
        );
    }

    /**
     * 默认接口服务
     * @desc 默认接口服务，当未指定接口服务时执行此接口服务
     * @return string title 标题
     * @return string content 内容
     * @return string version 版本，格式：X.X.X
     * @return int time 当前时间戳
     * @exception 400 非法请求，参数传递错误
     */
    public function index() {
        $info = array(
            'title' => 'Hello ' . $this->username,
            'version' => PHALAPI_VERSION,
            'time' => $_SERVER['REQUEST_TIME'],
            'ip' => $_SERVER['REMOTE_ADDR'],
        );
        return $info;
    }

    /**
     * 通用多条件查询接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function select_where()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');
        return \App\getInfoByPageByWhere($this);
    }

    /**
     * 删除接口
     * @desc 更新id删除接口
     * @return array data 删除状态
     * @exception 400 非法请求，参数传递错误
     */
    public function delete()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');
        return \App\deleteById($this);
    }


    /**
     * 更新,无id则创建
     * @desc 更新及新建接口服务
     * @return array data 数据内容
     * @return int count 当前条件下总数
     * @exception 400 非法请求，参数传递错误
     */
    public function update()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');
        return \App\updateByIdThis($this);
    }

    /**
     * 分页读取
     * @desc 按分页读取数据接口服务
     * @return array data 数据内容
     * @return int count 当前条件下总数
     * @exception 400 非法请求，参数传递错误
     */
    public function select()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');
        return \App\getInfoByPage($this);
    }
}
